package com.boss.salary.dao.impl;

import com.boss.framework.dao.BaseDaoImpl;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.dao.IPersonSalaryExtDao;
import com.boss.salary.entity.PersonSalaryExt;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class PersonSalaryExtDaoImpl extends BaseDaoImpl implements IPersonSalaryExtDao {

    private final static String TABLE_NAME = "person_salary_ext";

    @Override
    public List<PersonSalaryExt> queryPersonSalaryExtPage(PaginationDTO paginationDTO) {
        StringBuilder sql = new StringBuilder();
        sql.append(" select ")
                .append(" id, create_time, update_time, per_salary_id, agency_salary_id, salary_num, is_deleted")
                .append(" from ").append(TABLE_NAME);
        sql.append(" order by create_time");
        String querySql = this.boxUpSql(sql.toString());
        //请求参数初始化
        Object[] params = this.boxUpParam(null, paginationDTO);
        return this.getJdbcTemplate().query(querySql, new BeanPropertyRowMapper(PersonSalaryExt.class), params);
    }

    @Override
    public int queryPersonSalaryExtTotal() {
        StringBuilder sql = new StringBuilder();
        sql.append("select count(*) from ").append(TABLE_NAME);
        return this.getJdbcTemplate().queryForObject(sql.toString(), Integer.class);
    }

    @Override
    public void savePersonSalaryExt(PersonSalaryExt personSalaryExt) {
        StringBuilder sql = new StringBuilder();
        sql.append("insert into ").append(TABLE_NAME);
        sql.append("(id, create_time, update_time, per_salary_id, agency_salary_id, salary_num, is_deleted) ");
        sql.append("values (?,?,?,?,?,?,?)");
        Object[] params = new Object[]{
                personSalaryExt.getId(), personSalaryExt.getCreateTime(), personSalaryExt.getUpdateTime(),
                personSalaryExt.getPerSalaryId(), personSalaryExt.getAgencySalaryId(), personSalaryExt.getSalaryNum(),
                personSalaryExt.getIsDeleted()
        };
        this.getJdbcTemplate().update(sql.toString(), params);
    }

    @Override
    public void updatePersonSalaryExt(PersonSalaryExt personSalaryExt) {
        StringBuilder sql = new StringBuilder();
        sql.append("update ").append(TABLE_NAME);
        sql.append(" set update_time=?, per_salary_id=?, agency_salary_id=?, salary_num=?,")
                .append(" is_deleted=? where id=?");
        Object[] params = new Object[]{
                personSalaryExt.getUpdateTime(), personSalaryExt.getPerSalaryId(), personSalaryExt.getAgencySalaryId(),
                personSalaryExt.getSalaryNum(), personSalaryExt.getIsDeleted(), personSalaryExt.getId()
        };
        this.getJdbcTemplate().update(sql.toString(), params);
    }

    @Override
    public void deletePersonSalaryExt(String id) {
        StringBuilder sql = new StringBuilder();
        sql.append("update ").append(TABLE_NAME);
        sql.append(" set is_deleted='1' ")
                .append("where id=?");
        Object[] params = new Object[]{
                id
        };
        this.getJdbcTemplate().update(sql.toString(), params);
    }
}
